package com.zmn.oms.services.impl.detail;

import com.vdurmont.emoji.EmojiParser;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.oms.common.constant.OrderPayConsts;
import com.zmn.oms.model.entity.detail.OrderDetail;
import com.zmn.oms.model.vo.order.EsOrderDetailVO;
import com.zmn.oms.persistence.interfaces.detail.OrderDetailDao;
import com.zmn.oms.services.interfaces.detail.OrderDetailService;
import com.zmn.oms.services.interfaces.orderencrypt.OrderEncryptService;
import com.zmn.pay.common.constant.PayConsts;
import com.zmn.saas.common.utils.EmojiUtil;
import io.shardingsphere.api.HintManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 类描述：订单详情
 *
 * @author liuying
 * @date 2018/11/06 11:41
 */
@Slf4j
@Service
public class OrderDetailServiceImpl implements OrderDetailService {

    @Resource
    private OrderDetailDao orderDetailDao;

    @Autowired
    private OrderEncryptService orderEncryptService;

    @Override
    public List<EsOrderDetailVO> listEsOrderDetailByOrderIdList(List<Long> orderIdList) {
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();
            List<OrderDetail> orderDetails = orderDetailDao.listEsOrderDetailByOrderIdList(orderIdList);
            List<EsOrderDetailVO> esOrderDetailVOS = BeanMapper.mapList(orderDetails, EsOrderDetailVO.class);
            // 批量解密电话和地址
            orderEncryptService.batchDecryptEsVoPhoneAndAddress(esOrderDetailVOS);
            return esOrderDetailVOS;
        }
    }

    @Override
    public List<OrderDetail> listOrderDetailByOrderIdList(List<Long> orderIdList) {
        List<OrderDetail> orderDetails = orderDetailDao.listEsOrderDetailByOrderIdList(orderIdList);
        // 批量解密电话和地址
        orderEncryptService.batchDecryptPhoneAndAddress(orderDetails);
        return orderDetails;
    }

    @Override
    public OrderDetail findOrderDetailByKey(Long detailId) {
        OrderDetail orderDetail = orderDetailDao.findOrderDetailByKey(detailId);
        if (Objects.nonNull(orderDetail)) {
            OrderDetail decryptDetail = orderEncryptService.getDecryptPhoneAndAddress(orderDetail);// 解密电话和地址
            log.debug("readonly 解密结束:{}", detailId);
            return decryptDetail;
        } else {
            return findOrderDetailByKeySrcMaster(detailId);
        }
    }

    @Override
    public OrderDetail findDynamicOrderDetailByKey(String queryStr, Long detailId) {
        OrderDetail orderDetail = orderDetailDao.findDynamicOrderDetailByKey(queryStr, detailId);
        if (Objects.nonNull(orderDetail)) {
            OrderDetail decryptDetail = orderEncryptService.getDecryptPhoneAndAddress(orderDetail);// 解密电话和地址
            log.debug("readonly 解密结束:{}", detailId);
            return decryptDetail;
        } else {
            return findOrderDetailByKeySrcMaster(detailId);
        }
    }

    @Override
    public OrderDetail findOrderDetailByKeySrcMaster(Long detailId) {
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();
            OrderDetail orderDetail = orderDetailDao.findOrderDetailByKey(detailId);
            if (orderDetail != null && StringUtil.isNotBlank(orderDetail.getRemark())) {// 字符转换为表情符号
                orderDetail.setRemark(EmojiParser.parseToUnicode(orderDetail.getRemark()));
            }
            OrderDetail decryptDetail = orderEncryptService.getDecryptPhoneAndAddress(orderDetail);// 解密电话和地址
            log.debug("master 解密结束:{}", detailId);
            return decryptDetail;
        }
    }

    @Override
    public Integer updateOrderDetailScopeServiceByKey(Long detailId, Integer inScopeService) {
        Objects.requireNonNull(detailId);
        Objects.requireNonNull(inScopeService);

        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setDetailId(detailId);
        orderDetail.setInScopeService(inScopeService);
        // 加密电话和地址
        OrderDetail encryptOrderDetail = orderEncryptService.getEncryptOrderDetail(orderDetail);
        return orderDetailDao.updateByKey(encryptOrderDetail);
    }

    @Override
    public Integer updateOrderDetailByKey(OrderDetail orderDetail) {
        // 加密电话和地址
        OrderDetail encryptOrderDetail = orderEncryptService.getEncryptOrderDetail(orderDetail);
        orderDetail.setUpdateTime(Optional.ofNullable(orderDetail.getUpdateTime()).orElse(DateUtil.getNow()));
        return orderDetailDao.updateByKey(encryptOrderDetail);
    }

    @Override
    public Integer updateOrderDetailAddressByKey(OrderDetail orderDetail) {
        // 加密电话和地址
        OrderDetail encryptOrderDetail = orderEncryptService.getEncryptOrderDetail(orderDetail);
        return orderDetailDao.updateAddressByKey(encryptOrderDetail);
    }

    @Override
    public Integer insertOrderDetail(OrderDetail orderDetail) {

        if (StringUtil.isNotBlank(orderDetail.getRemark())) {// 表情符号转换为字符
            orderDetail.setRemark(EmojiUtil.filterEmoji(orderDetail.getRemark()));
        }
        if (StringUtil.isNotBlank(orderDetail.getAddress())) {// 地址表情符号转换
            orderDetail.setAddress(EmojiUtil.filterEmoji(orderDetail.getAddress()));
        }
        Date createTime = Optional.ofNullable(orderDetail.getCreateTime()).orElse(DateUtil.getNow());
        orderDetail.setCreateTime(createTime);
        orderDetail.setUpdateTime(createTime);
        // 加密电话和地址
        OrderDetail encryptOrderDetail = orderEncryptService.getEncryptOrderDetail(orderDetail);
        return orderDetailDao.insertOrderDetail(encryptOrderDetail);
    }

    @Override
    public String getMobile(OrderDetail orderDetail) {
        if (StringUtil.isMobile(orderDetail.getTelephone())) {
            return orderDetail.getTelephone();
        }
        else if (StringUtil.isMobile(orderDetail.getTelephone2())) {
            return orderDetail.getTelephone2();
        }
        else if (StringUtil.isMobile(orderDetail.getTelephone3())) {
            return orderDetail.getTelephone3();
        }

        return null;
    }
}
